window.onload = function() {
  var canvas = document.getElementById("canvas");
  var ctx = canvas.getContext("2d");
  
  // var W = window.innerWidth; var H = window.innerHeight;
  canvas.style.width = "1920px";
  canvas.style.height = "1080px";
   W = canvas.offsetWidth;
  H = canvas.offsetHeight;
  canvas.width = W;
  canvas.height = H;
  var particles = [];
  for (var i = 0; i < 70; i++) {
    particles.push(new Particle());
  }
  
  function Particle() {
    this.location = {x: Math.random()*W, y: Math.random()*H};
    this.radius = 0;
    this.speed = 2;
    this.angle = Math.random()*360;
    
    var r = Math.round(Math.random()*255);
    var g = Math.round(Math.random()*255);
    var b = Math.round(Math.random()*255);
    var a = Math.random()*0.2;
    this.rgba = "rgba("+ r +", "+ g +","+ b +", " + a + ")";
     
  }
  
  function draw() {
    
    ctx.globalCompositeOperation = "source-over";
    ctx.fillStyle = "rgba(1, 1, 1, 0.05)";
    ctx.fillRect(0, 0, W, H);
    ctx.globalCompositeOperation = "lighter";
    for (var i = 0; i < particles.length; i++) {
      var p = particles[i];
      ctx.fillStyle = "white";
      ctx.fillRect(p.location.x, p.location.y, p.radius, p.radius);
      
      for (var n = 0; n < particles.length; n++) {
        var p2 = particles[n];
        var yd = p2.location.y - p.location.y;
        var xd = p2.location.x - p.location.x;
        var distance = Math.sqrt(xd*xd + yd*yd);
        
        if (distance < 300) {
          ctx.beginPath();
          ctx.lineWidth = 1;
          ctx.moveTo(p.location.x, p.location.y);
          ctx.lineTo(p2.location.x, p2.location.y);
          ctx.strokeStyle = p.rgba;
          ctx.stroke();
        }
      }
      
      p.location.x = p.location.x + p.speed*Math.cos(p.angle*Math.PI/180);
      p.location.y = p.location.y + p.speed*Math.sin(p.angle*Math.PI/180);
    
    	if (p.location.x < 0) p.location.x = W + 50;
      if (p.location.x > W + 50) p.location.x = 0;
      if (p.location.y < 0) p.location.y = H + 50;
      if (p.location.y > H + 50) p.location.y = 0;
      
    }
  }
  
  setInterval(draw, 3);
}